
// 一、 初始化地图


// ----打开fmap地图start---- //
var fn_map
var open_pngmap=function(div_id,map_detail,user_self_name){
  var content=map_detail.content
  var img=document.createElement("IMG")
  img.src="/static/map_data/"+user_self_name+'/'+content
  $('#'+div_id).append(img)
}

// ----打开fmap地图end---- //



// 地图地址搜索功能 
var searchAnalyser;
var select_obj;
var search_map=function(self){
  if(select_obj!=null){
    select_obj.selected=false
  }
  addr=self.value.trim();
  // console.log(addr)
  if(addr==''){
    layer.msg('请输入地址')
    return
  }

}

//查询周边信息
var trakcer_addr_obj={}
search_by_xy=function(device_id,x,y,gid=1){
  console.log('device_id')
}

// var tracker_layer
// var tracker_name_layer
// var tracker_marker_obj={}
// var tracker_marker_name_obj={}

var add_tracker_marker=function(obj,div_id){
  // console.log(obj)
  var device_id=obj.device_id
  var user_name=obj.user_name
  var icon_show='/static/show_icon/'+obj.user_detail.icon+'.png'
  var x=parseFloat(obj.dynamic.lng);
  var y=parseFloat(obj.dynamic.lat);

  var marker = '<div id="'+device_id+'" style="position:absolute;left:'+x+'px;top:'+y+'px;width:80px;height:80px" >'+user_name+
              '<img name="'+device_id+',device" src="'+icon_show+'"  style="width:40px;height:40px"></div>'
  $('#'+div_id).append(marker)

}


// 添加静态覆盖物，建筑，基站等
// var static_marker_layer
// var static_marker_name_layer
var static_marker_obj={};
// var static_marker_name_obj={};

var add_static_marker=function(obj,kind,div_id){
  // console.log(obj,kind)
  // var kind=obj.kind
  var unique='device_id'
  var num=obj.scann_data
  if(obj.device_id==null){
    unique='name'
    num=obj.asset.length.toString()
  }
  var icon_dict={'building':'/static/show_icon/normal.png','camera':'/static/show_icon/camera.png','scanner':'/static/show_icon/scanner.png','tag':'/static/show_icon/person/green_man.png'}
  var icon_show=icon_dict[kind]

  var icon;
  icon=eval("("+obj.param_set+")")
  icon=icon.icon
  if (icon!=null){
    icon_show='/static/show_icon/'+icon+'.png'
  }
  var posi=[0,0]
  if(obj['position']!='0' && obj['position']!=null){
    posi=eval('('+obj['position']+')').location
    posi=posi.split(',')  
  }
  var x=parseFloat(posi[0]);
  var y=parseFloat(posi[1]);

  var marker = '<div id="'+obj[unique]+'" style="position:absolute;left:'+x+'px;top:'+y+'px;width:80px;height:80px" ><div>'+obj[unique]+'</div>'+
              '<img name="'+obj[unique]+','+kind+'" src="'+icon_show+'"  style="width:40px;height:40px"></div>'
  $('#'+div_id).append(marker)
  // var marker1 ='<img src="/static/img/move.gif"  style="position:absolute;left:'+x+'px;top:'+y+'px;width:60px;height:80px">'
  // $('#'+div_id).append(marker1)
  obj.show=true
  obj.overlay_name=[obj[unique]]
  obj.overlay_kind=kind
  static_marker_obj[obj[unique]]=obj

}

// ----添加marker函数 end----- //

// 删除tracker图标
var remove_tracker_marker=function(div_id){

  // 要把信息弹窗先关掉，不然报错
  layer.closeAll()
  $('#'+div_id).remove()

}

// 隐藏/展示tracker图标
var hide_tracker_marker=function(device_id,value){
  // console.log(device_id)
  $('#'+device_id).hide()
}

// 移动tracker和名称
var move_tracker_marker=function(device_id,x,y){
  // x=parseFloat(x);
  // y=parseFloat(y);
  // var move = "position:absolute;left:'+x+'px;top:'+y+'px;width:40px;height:80px"
  $('#'+device_id).attr('style','position:absolute;left:'+x+'px;top:'+y+'px;width:80px;height:80px')
}

// 删除static图标

var remove_static_marker=function(div_id){
  $('#'+div_id).remove()
}
// 隐藏/显示固定的marker
var hide_static_marker=function(kind,value){
  var unique='name'
  if(kind!=='building'){
    unique='device_id'
  }
  var data=eval(kind)
  if(value){
    data.forEach(item=>{
      $('#'+item[unique]).show()
    })
  }else{
    data.forEach(item=>{
      $('#'+item[unique]).hide()
    })
  }
  
}

// ----展示/隐藏marker信息窗 start----- //

var all_static_infordow={}

var static_marker_inforwindow=function(obj_name,obj_kind){
  // console.log(obj)
  // var obj_name=obj.overlay_name
  // var obj_kind=obj.overlay_kind
  var table_id=obj_kind+obj_name
  var window_content='<div style="padding:10px"><table id="'+table_id+'" >' + '</table></div>';
  var obj_detail=get_overlay_detail(obj_kind,obj_name)
  var posi=[0,0]
  if(obj_detail['position']!='0' && obj_detail['position']!=null){
    posi=eval('('+obj_detail['position']+')').location
    posi=posi.split(',')  
  }
  var x=posi[0]
  var y=posi[1]
  var kind_cn={'building':'建筑信息','scanner':'基站信息'}
  layer.open({
            type: 1
            ,title:kind_cn[obj_kind]
            ,offset:[y+'px',x+'px']  
            ,id: 'static_marker_inforwindow' //防止重复弹出
            ,content:window_content
            ,shade: 0 //不显示遮罩
            ,success: function(layero, index){
                                              }
            
          });
  //添加弹框到地图上，绑定marker
  

  var table_data=[]
  var table_cols;
  
  if(obj_kind=='building'){
    console.log(obj_name)
    var build=building_obj[obj_name]
    if (build.asset!='0') {
      for(var i=0;i<build.asset.length;i++){
        var tag=get_detail_from_id(device,build.asset[i])
        // tag.serv_receive=$('#get_current_time').text()
        table_data.push(tag)
      }
    }
    var param_set=eval('('+obj_detail.param_set+')')
    table_cols=[[{ "title": "建筑名字： " + obj_name, "colspan": 3 },
            { "title": "权限： " + param_set.auth_in, "colspan": 3 },],
            [{ "title": "人数上限： " + param_set.person_limit + "人", "colspan": 3 },
            { "title": "停留上限： " + param_set.time_limit + "分", "colspan": 3 }],
            [{ "title": "建筑明细  " + '(当前:' + build.asset.length.toString() + ')', "colspan": 6 }],
            [{ field: 'user_name', title: '名称' },
            // { field: 'department', title: '归属' },
            {
              field: 'serv_receive', title: '进入时间'//, formatter: function (index, row, value) {
            },
            {
              field: 'stop_time', title: '停留时长', //formatter: function (index, row, value) {
            },
            { field: 'device_id', title: '设备编号' }]
            ]
  }else if(obj_kind=='scanner'){
    var sc=scanner_obj[obj_name]
    // console.log(sc)
    if(sc.scann_data!='0'){
      var scann_data=eval('('+sc.scann_data+')')
      // var scann_data[scann_data.length-1]
      rt=scann_data.pop()
      // console.log(scann_data,rt)
      scann_data.forEach(item=>{
        item.serv_receive=rt
      })
      table_data=scann_data

    }
    table_cols=[[{ "title": "设备名称： " + obj_name, "colspan": 2 },{ "title": "设备ID： " + obj_detail.device_id, "colspan": 2 },],
                [{ "title": "设备类型： " +  obj_detail.kind, "colspan": 2 },{ "title": "设备状态： " + obj_detail.status , "colspan": 2 }],
                [{ "title": "运行详情:  ", "colspan": 4 }],
                [{ field: 'device_id', title: '标签编号' },{ field: 'rssi', title: '信号强度' },{field: 'serv_receive', title: '时间'},{field: 'remark', title: '备注'}]
               ]
  }
  
  if(obj_kind!='camera'){
    table.render({
      elem: '#'+table_id,
      data: table_data,
      id:table_id+'_show',
      height: 360,
      cols: table_cols,
      // limits: [10, 15, 20, 25, 50, 100],
      limit: 10000000,
      // page: true
    });
  }
}

var trakcer_marker_infowindow_obj={}
var tracker_marker_inforwindow=function (device_id) {
  // 绑定的用户信息
  var dev=get_detail_from_id(device,device_id)
  var rssi=dev.dynamic.rssi
  var x=(dev.dynamic.lng+240).toString()
  var y=(dev.dynamic.lat-170).toString()
  var dev_loca=dev.dynamic.location;
  if(trakcer_addr_obj[dev.device_id]!=null){
    dev_loca=trakcer_addr_obj[dev.device_id]
  }
  var user_detail=dev.user_detail;
  var dev_user_name=dev.user_name;
  var param1=user_detail.customize_title1+'：'+user_detail.customize_value1
  var param2=user_detail.customize_title2+'：'+user_detail.customize_value2
  var param3=user_detail.customize_title3+'：'+user_detail.customize_value3
  var tracker_info_window=
   "<div style='margin:0;padding:10px;border-radius:20px'>"+
    "<table class='layui-table' lay-size='sm'  style='font-size:10px;margin:0px 0px 0px 0px;padding:0px 0px 0px 0px;'>"+
    "<tbody>"+
    "<tr>"+
      "<td colspan=2 bgcolor='#009688' style='height:15px'><font color='white'>"+dev_user_name+','+param1+','+param2+','+param3+"</font></td>"+
    "</tr>"+

    "<tr >"+
    "<td colspan=2 bgcolor='#009688' style='height:15px'><font color='white'>"+"设备ID : "+dev.device_id+"</font></td>"+
    "</tr>"+
    "<tr>"+
    "<td colspan=2>"+
    "<div class='layui-row'>"+
    "<div class='layui-col-lg4'>"+dev.status+"</div>"+
    "<div class='layui-col-lg8'>"+
    "<i class='fa fa-battery-full pull-right' style = 'font-size:10px; padding-right: 9px;color: #009688;' > "+dev.battery+"</i>"+
    "<i class='fa fa-broadcast-tower pull-right'  style = 'font-size:10px; padding-right: 9px;color: #009688;'>"+rssi+"</i>"+
    "</div>"+
    "</div>"+
    "</td>"+
    "<tr>"+
    "<td colspan=2>"+" <i class='layui-icon layui-icon-console' aria-hidden='true' style='color:#1E9FFF'></i>："+$('#get_current_time').text()+"</td>"+
    "</tr>"+
    "<tr>"+
    "<td colspan=2>"+" <i class='fa fa-map-marker' aria-hidden='true' style='color:#5FB878'></i>："+dev.dynamic.track_type+"</td>"+
    "</tr>"+
    "<tr>"+
    "<td colspan=2>"+"<i class='fa fa-map' aria-hidden='true' style='color:#009688'></i>："+dev_loca+"</td>"+
    "</tr>"+

      "</tbody>"+
  "</table>"+ 
    "<br>"+
    "<button class='layui-btn layui-btn-xs' onclick='turn_to_page(\"fmap_trail\")'>"+
      '<i class="layui-icon ">&#xe715;</i>'+
      "轨迹"+
    "</button>"+
    "<button class='layui-btn layui-btn-xs ' onclick='set_param("+dev.device_id+")'>"+
      '<i class="layui-icon ">&#xe667;</i>'+
      "报警设置"+
    "</button>"+
    // "<button class='layui-btn layui-btn-xs' onclick='command_modal("+dev.device_id+","+g_status+")'>"+
    //   '<i class="layui-icon ">&#xe601;</i>'+
    //   "命令"+
    // "</button>"+
    // "<button class='layui-btn layui-btn-xs' onclick='device_set_modal("+dev.device_id+")'>"+
    //   '<i class="layui-icon ">&#xe614;</i>'+
    //   "设置"+
    // "</button>"+
    // "<button class='layui-btn layui-btn-xs' onclick='turn_to_page(\"interact\")'>"+
    //   '<i class="layui-icon ">&#xe64c;</i>'+
    //   "互动"+
    // "</button>"+
  "</div>"
  ;
  layer.open({
            type: 1
            ,title:false
            // ,area:['400px','500px']
            ,offset:[y+'px',x+'px']  
            ,id: 'tracker_info_window' //防止重复弹出
            ,content:tracker_info_window
            ,shade: 0 //不显示遮罩
            ,success: function(layero, index){
                                              }
            
          });
}
// ----展示/隐藏marker信息窗 end----- //